package com.gang.ssm.bos.system.service.impl;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gang.ssm.bos.system.bean.Noticebill;
import com.gang.ssm.bos.system.bean.NoticebillExample;
import com.gang.ssm.bos.system.bean.NoticebillExample.Criteria;
import com.gang.ssm.bos.system.bean.Workbill;
import com.gang.ssm.bos.system.mapper.DecidedzoneMapper;
import com.gang.ssm.bos.system.mapper.NoticebillMapper;
import com.gang.ssm.bos.system.mapper.WorkbillMapper;
import com.gang.ssm.bos.system.service.CustomerService;
import com.gang.ssm.bos.system.service.NoticebillService;

@Service
public class NoticebillServiceImpl implements NoticebillService {

	@Autowired
	private NoticebillMapper noticebillMapper;
	
	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private DecidedzoneMapper decidedzoneMapper;
	 
	@Autowired
	private WorkbillMapper workbillMapper;
	
	@Override
	public void save(Noticebill noticebill, Integer decidedzoneId) {
		noticebillMapper.insertSelective(noticebill);
		
		if(decidedzoneId != null) {
			Integer staffId = decidedzoneMapper.findStaffIdById(decidedzoneId);
			noticebill.setStaffId(staffId);
			noticebill.setOrdertype("自动");
			
			Workbill workbill = new Workbill();
			workbill.setAttachbilltimes(0);
			workbill.setBuildtime(new Timestamp(System.currentTimeMillis()));
			workbill.setPickstate("未取件");
			workbill.setNoticebillId(noticebill.getId());
			workbill.setRemark(noticebill.getRemark());
			workbill.setStaffId(staffId);
			workbill.setType("新单");
			
			workbillMapper.insertSelective(workbill);
		}else{
			noticebill.setOrdertype("手动");
		}
		
		noticebillMapper.updateByPrimaryKeySelective(noticebill);
	}

	@Override
	public List<Noticebill> findnoassociations() {
		NoticebillExample noticebillExample = new NoticebillExample();
		Criteria criteria = noticebillExample.createCriteria();
		criteria.andStaffIdIsNull();
		return noticebillMapper.selectByExample(noticebillExample);
	}

	@Override
	public void editStaffId(Integer id, Integer staffId) {
		noticebillMapper.editStaffId(id,staffId);
		String remark = noticebillMapper.findRemarkById(id);
		
		Workbill workbill = new Workbill();
		workbill.setAttachbilltimes(0);
		workbill.setBuildtime(new Timestamp(System.currentTimeMillis()));
		workbill.setNoticebillId(id);
		workbill.setPickstate("未取件");
		workbill.setRemark(remark);
		workbill.setStaffId(staffId);
		workbill.setType("新单");
		
		workbillMapper.insertSelective(workbill);
	}

}
